﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes.actions {
    import Engine.Helpers.*;
    import Classes.*;
    import com.zynga.skelly.util.*;

    public class ActionNavigateWander extends ActionNavigate {

        protected static const FAIL_PAUSE_TIME:Number = 3;

        protected var m_elapsed:Number = 0;
        protected var m_timeout:Number = -1;
        protected var m_idleTime:Number = 0;
        protected var m_previousTarget:MapResource;
        protected var m_numRerolls:int = 3;
        protected var m_continueAfterFail:Boolean = true;

        public function ActionNavigateWander(_arg1:NPC, _arg2:MapResource=null, _arg3:Number=0){
            this.m_previousTarget = _arg2;
            this.m_elapsed = _arg3;
            super(_arg1, null, this.m_previousTarget);
        }
        public function cleanUp():void{
            if (m_npc){
                m_npc.getStateMachine().removeState(this);
            };
        }
        public function setIdleTime(_arg1:Number):ActionNavigateWander{
            this.m_idleTime = _arg1;
            return (this);
        }
        public function setTimeout(_arg1:Number):ActionNavigateWander{
            this.m_timeout = _arg1;
            return (this);
        }
        public function setTarget(_arg1:MapResource):ActionNavigateWander{
            m_target = _arg1;
            return (this);
        }
        public function setFailureCorrection(_arg1:Boolean):ActionNavigateWander{
            this.m_continueAfterFail = _arg1;
            return (this);
        }
        public function get expired():Boolean{
            return ((((this.m_timeout >= 0)) && ((this.m_elapsed > this.m_timeout))));
        }
        override public function enter():void{
            this.onStart();
            super.enter();
        }
        override public function reenter():void{
            this.m_elapsed = 0;
            this.onStart();
            super.reenter();
        }
        protected function onStart():void{
            if ((m_npc is Vehicle)){
                GlobalEngine.warning("ActionNavigateWander", "This action was not designed for vehicles.");
                this.cleanUp();
                return;
            };
            if (!m_target){
                m_target = Global.world.citySim.roadManager.findRandomWalkableTileNearEdges();
            };
        }
        override protected function updatePath(_arg1:Boolean):void{
            super.updatePath(_arg1);
            if ((m_target is Sidewalk)){
                m_path.pop();
            };
        }
        override public function update(_arg1:Number):void{
            var newStates:* = null;
            var dt:* = _arg1;
            this.m_elapsed = (this.m_elapsed + dt);
            if (!this.expired){
                if (m_npc){
                    if (((((!(m_path)) || ((m_path.length == 0)))) && (!(this.m_navigationFailed)))){
                        newStates = [new ActionFn(m_npc, Curry.curry(function (_arg1:NPC):void{
                            _arg1.animation = "static";
                        }, m_npc)), new ActionNavigateWander(m_npc, m_target, this.m_elapsed).setIdleTime(this.m_idleTime).setTimeout(this.m_timeout)];
                        if (this.m_idleTime > 0){
                            newStates.unshift(new ActionFn(m_npc, Curry.curry(function (_arg1:NPC):void{
                                _arg1.animation = "idle";
                            }, m_npc)), new ActionPause(m_npc, this.m_idleTime));
                        };
                        m_npc.getStateMachine().insertActionsArray(Math.max(0, (m_npc.getStateMachine().getStates().length - 1)), newStates);
                    };
                };
            };
            super.update(dt);
            if (this.expired){
                this.cleanUp();
            };
        }
        override protected function doNavigationFailed():void{
            var collider:* = null;
            var box:* = null;
            super.doNavigationFailed();
            if (!this.m_continueAfterFail){
                return;
            };
            m_npc.clearStates();
            var rolls:* = 0;
            var colliders:* = Global.world.getObjectsByClassAt(MapResource, m_npc.getPosition());
            var closest:* = Global.world.citySim.roadManager.findClosestWalkableTile(m_npc.getPosition());
            var standing:* = null;
            if (colliders.length == 0){
                box = new Box3D(new Vector3((m_npc.getPosition().x - 0.5), (m_npc.getPosition().y - 0.5)), new Vector3(1.7, 1.7));
                colliders = Global.world.getCollisionMap().getIntersectingObjects(box);
            };
            for each (collider in colliders) {
                if ((((collider is Sidewalk)) || ((collider is Road)))){
                    standing = closest;
                    while ((((standing == closest)) && ((rolls < this.m_numRerolls)))) {
                        standing = Global.world.citySim.roadManager.findRandomWalkableTile(closest);
                        rolls = (rolls + 1);
                    };
                    break;
                };
            };
            m_npc.animation = "static";
            if (((!((standing == closest))) && (!((standing == null))))){
                m_npc.getStateMachine().addActions(new ActionFn(m_npc, function ():void{
                    m_npc.animation = "static";
                }), new ActionNavigateWander(m_npc, closest, this.m_elapsed).setTarget(standing).setIdleTime(this.m_idleTime).setTimeout(this.m_timeout));
                return;
            };
            if ((((standing == null)) && (!((closest == null))))){
                m_npc.getStateMachine().addActions(new ActionFn(m_npc, function ():void{
                    m_npc.animation = "static";
                }), new ActionNavigateBeeline(m_npc, ((closest is Road)) ? closest.getHotspot() : closest.getPosition().add(this.pseudoSidwalkSize), null));
            };
            m_npc.getStateMachine().addActions(new ActionFn(m_npc, function ():void{
                m_npc.animation = "idle";
            }), new ActionPause(m_npc, ((this.m_idleTime > 0)) ? this.m_idleTime : ActionNavigateWander.FAIL_PAUSE_TIME));
            m_npc.getStateMachine().addActions(new ActionFn(m_npc, function ():void{
                m_npc.animation = "static";
            }), new ActionNavigateWander(m_npc, closest, this.m_elapsed).setIdleTime(this.m_idleTime).setTimeout(this.m_timeout));
        }
        protected function get pseudoSidwalkSize():Vector3{
            return (new Vector3(0.5, 0.5));
        }

    }
}//package Classes.actions 
